<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<head>
</head>
<body>
<article class="page-container" id="mainArt">
	<form id="form1" class="form form-horizontal" action="${ctx}/user/saveEntity.do" method="post" enctype="multipart/form-data">
		<c:if test="${entity.id != null}"><input type="hidden" id="entityID" name="id" value="${entity.id }" /></c:if>
		<div class="row cl">
			<label class="form-label col-xs-2"><span class="c-red">*</span>登录账号:</label>
			<div class="formControls col-xs-4">
				<input type="text" class="input-text required account" value="${entity.account }" placeholder="建议不要使用汉字"
				<c:if test="${entity.id != null}">readonly</c:if> id="account" name="account"/>
			</div>
			<label class="form-label col-xs-2"><span class="c-red">*</span>昵称:</label>
			<div class="formControls col-xs-4">
				<input id="nickName" name="nickName" class="input-text required" value="${entity.nickName }"  type="text"/>
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-2"><span class="c-red">*</span>用户类型:</label>
			<div class="formControls col-xs-4">
				<span class="select-box">
					<select class="select" id="userType" name="userType" size="1" onchange="selectAmdin();">
							<%--<option value="">-- 请选择用户类型 --</option>--%>
							<option value="0" <c:if test="${entity.userType == '0'}">selected</c:if>>普通用户</option>
							<%--<option value="1" <c:if test="${entity.userType == '1'}">selected</c:if>>教师</option>--%>
							<option value="2" <c:if test="${entity.userType == '2'}">selected</c:if>>管理员</option>
					</select>
				</span>
			</div>

			<label class="form-label col-xs-2"><span class="c-red">*</span>锁定登录:</label>
			<div class="formControls col-xs-4">
				<span class="select-box">
					<select class="select" size="1" name="available" id="available">
						<option value="false" <c:if test="${entity.available == 'false' ||  entity.available == null}">selected</c:if> >正常</option>
						<option value="true" <c:if test="${entity.available == 'true' }">selected</c:if> >锁定</option>
					</select>
				</span>
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-2"><span class="c-red">*</span>性别:</label>
			<div class="formControls col-xs-4">
				<span class="select-box">
					<select class="select" id="sex" name="sex" size="1">
						<option value="0" <c:if test="${entity.sex == '0' ||  entity.available == null}">selected</c:if> >女</option>
						<option value="1" <c:if test="${entity.sex == '1' }">selected</c:if> >男</option>
					</select>
				</span>
			</div>

			<label class="form-label col-xs-2"><span class="c-red">*</span>生日:</label>
			<div class="formControls col-xs-4">
				<input type="text" onfocus="WdatePicker({dateFmt:'yyyy-MM-dd'})" id="birthday" class="input-text Wdate required"
					   style="width:200px;" name="birthday" value="<fmt:formatDate value="${entity.birthday}"  pattern="yyyy-MM-dd"/>"/>
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-2">头像:</label>
			<div class="formControls col-xs-10">
				<span class="btn-upload form-group">
				  <input value="${entity.photo }" class="input-text upload-url radius" placeholder="建议:750*336" type="text" name="uploadfile-1" id="uploadfile-1" readonly>
				  <a href="javascript:void(0);" class="btn btn-primary radius upload-btn"><i class="Hui-iconfont">&#xe642;</i> 浏览文件</a>
				  <input type="file"  name="file" class="input-file" accept="image/*">
				</span>
				<c:if test="${!empty entity.photo }">
					<div>
						<c:if test="${empty imgPaths }">
							<img src="${entity.photo}" width="20%" />
						</c:if>
						<c:if test="${not empty imgPaths }">
							<c:forEach items="${imgPaths }" var="img" >
								<img src="${img}" width="20%" />
							</c:forEach>
						</c:if>
					</div>
				</c:if>
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-2">地址:</label>
			<div class="formControls col-xs-3">
				<input type="text" class="input-text" style="display:none;" value="${entity.address }"  id="address" name="address"/>
				<select id="s_province"  class="select"></select>
			</div>
			<div class="formControls col-xs-3">
				<select id="s_city" class="select"></select>
			</div>
			<div class="formControls col-xs-4">
				<select id="s_county" class="select"></select>
			</div>
		</div>
		<div class="row cl" id="admin" style="display: none;">
			<label class="form-label col-xs-2">权限角色:</label>
			<div class="formControls col-xs-4">
				<span class="select-box">
					<sys:select url="${ctx}/role/findAllPublic" id="defalutRole" name="defalutRole.id"
								value="id" text="name" cssClass="select" selectValue="${entity.defalutRole.id }"/>
				</span>
			</div>
		</div>
		
		<div class="row cl">
			<div id="buttonDiv" class="col-xs-6 col-xs-offset-3 text-c">
				<c:if test="${entity.id != null}">
					<a href="javascript:submit();" class="btn btn-success radius"><i class="Hui-iconfont">&#xe60c;</i> 修改</a>
				</c:if>
				<c:if test="${entity.id == null}">
					<a href="javascript:submit();" class="btn btn-primary radius"><i class="Hui-iconfont">&#xe600;</i> 添加</a>
				</c:if>
			</div>
		</div>
	</form>
</article>
<script type="text/javascript">
	$(function(){
        var type=$("#userType").val();
        if(type=="2"){
            $("#admin").show();
        }
        _init_area();
        var address = "${entity.address}";
        if(address!=""){
            address=address.split("-");
            $("#s_province").val(address[0]);
            $("#s_province").trigger("change");
            if(address.length>1){
                $("#s_city").val(address[1]);
                $("#s_city").trigger("change");
            }
            if(address.length>2){
                $("#s_county").val(address[2]);
            }
        }
		$("#form1").validate({
			rules: {
                account: {remote: "${ctx}/user/checkUserName?id=${entity.id}"}
			},
			messages: {
                account: {remote: "用户名已存在.", required: "请填写用户名."}
			},
			onkeyup:false,
			focusCleanup:true,
			success:"valid",
			submitHandler:function(form){
				var index = layer.load();
                if($("#s_county").val()!="市、县级市"){
                    $("#address").val($("#s_province").val()+"-"+$("#s_city").val()+"-"+$("#s_county").val());
                }
				$(form).ajaxSubmit({
	                success:function(data){ //提交成功的回调函数  
	            	  	layer.close(index);  
	                    if(data.flag==200){
	    		        	layer.alert(data.msg,{icon:6},function(index){
	                			if(data.isUpdate == "false"){
	                				parent.location.reload();
	                			}else{
	                				parent.reloadTable();
	                			}
    		                	//关闭当前弹窗
    		    				var index = parent.layer.getFrameIndex(window.name);
    		    				parent.$('.btn-refresh').click();
    		    				parent.layer.close(index);
                				layer.close(index);
                			});
	    	       		} else{
	    	       			layer.alert(data.msg, {icon: 5});
	    	       		}
	                },
	                error:function(data){
	            		layer.close(index);  
	                	layer.alert("操作失败",{icon:5},function(index){
            				//刷新父级
            				parent.location.reload();
		                	//关闭当前弹窗
		    				var index = parent.layer.getFrameIndex(window.name);
		    				parent.$('.btn-refresh').click();
		    				parent.layer.close(index);
            				layer.close(index);
            			});     
	                }
				});
			}
		});
	});
	function submit(){
		$("#form1").submit();
	}

	function selectAmdin() {
		var type=$("#userType").val();
		if(type=="2"){
		    $("#admin").show();
		}else{
            $("#admin").hide();
            $("#defalutRole").val("");
		}

    }
</script>
</body>
